package com.labichaoka.chaoka.ui.home.fragment.my;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.labichaoka.chaoka.R;
import com.labichaoka.chaoka.base.BaseFragment;
import com.labichaoka.chaoka.base.MyApplication;
import com.labichaoka.chaoka.entity.GetDepositBalanceInfo;
import com.labichaoka.chaoka.entity.OverdueAndIdcardResponse;
import com.labichaoka.chaoka.ui.account.LoginActivity;
import com.labichaoka.chaoka.ui.bank.display.BankCardDisplayActivity;
import com.labichaoka.chaoka.ui.baseinfo.person.EmailActivity;
import com.labichaoka.chaoka.ui.home.fragment.my.feedback.FeedBackActivity;
import com.labichaoka.chaoka.ui.home.fragment.my.message.MessageActivity;
import com.labichaoka.chaoka.ui.home.fragment.my.record.WithDrawRecordActivity;
import com.labichaoka.chaoka.ui.home.fragment.my.set.SettingActivity;
import com.labichaoka.chaoka.ui.home.fragment.my.withdraw.WithdrawActivity;
import com.labichaoka.chaoka.ui.record.BorrowRecordActivity;
import com.labichaoka.chaoka.ui.web.WebViewActivity;
import com.labichaoka.chaoka.utils.DetectionUtil;
import com.labichaoka.chaoka.utils.SharedPreferencesManager;
import com.labichaoka.chaoka.utils.ToastUtils;
import com.sobot.chat.SobotApi;
import com.sobot.chat.api.model.Information;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.OnClick;

public class MyFragment extends BaseFragment implements MyFragmentView {
    @BindView(R.id.back_layout)
    RelativeLayout back;
    @BindView(R.id.right_icon)
    TextView right;
    @BindView(R.id.next)
    TextView next;
    @BindView(R.id.next1)
    TextView next1;
    @BindView(R.id.next2)
    TextView next2;
    @BindView(R.id.next3)
    TextView next3;
    @BindView(R.id.next4)
    TextView next4;
    @BindView(R.id.next5)
    TextView next5;
    @BindView(R.id.invite_icon)
    TextView inviteIcon;
    @BindView(R.id.custody_icon)
    TextView custodyIcon;
    @BindView(R.id.service_icon)
    TextView serviceIcon;
    @BindView(R.id.help_icon)
    TextView helpIcon;
    @BindView(R.id.kui_icon)
    TextView kuiIcon;
    @BindView(R.id.set_icon)
    TextView setIcon;
    @BindView(R.id.txt_user)
    TextView txt_user;
    @BindView(R.id.login_layout)
    LinearLayout loginLayout;
    @BindView(R.id.overdue_icon)
    ImageView overdueImg;
    @BindView(R.id.gold_layout)
    LinearLayout goldLayout;

    @BindView(R.id.msg_icon)
    ImageView msgIcon;
    @BindView(R.id.top_layout1)
    RelativeLayout topLayout1;
    @BindView(R.id.top_layout2)
    LinearLayout topLayout2;
    @BindView(R.id.amount)
    TextView amount;
    @BindView(R.id.hintMsg)
    TextView hintMsg;
    @BindView(R.id.withdraw)
    TextView withdraw;
    @BindView(R.id.txt_user2)
    TextView txtUser2;
    @BindView(R.id.login_layout2)
    LinearLayout loginLayout2;
    @BindView(R.id.msg_icon2)
    ImageView msgIcon2;
    @BindView(R.id.message_layout2)
    LinearLayout messageLayout2;
    @BindView(R.id.view)
    View view;
    @BindView(R.id.custody)
    RelativeLayout custody;

    private MyFragmentPresenter presenter;
    private String idcard;
    private Dialog dialog;
    private String isCanClick;
    private String clickMsg;
    private String appJumpUrl;
    private Bundle bundle;
    private String loginPhone;

    @Override
    protected int initLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView() {
        back.setVisibility(View.GONE);
        right.setTypeface(iconfont);
        next.setTypeface(iconfont);
        next1.setTypeface(iconfont);
        next2.setTypeface(iconfont);
        next3.setTypeface(iconfont);
        next4.setTypeface(iconfont);
        next5.setTypeface(iconfont);
        custodyIcon.setTypeface(iconfont);
        inviteIcon.setTypeface(iconfont);
        serviceIcon.setTypeface(iconfont);
        helpIcon.setTypeface(iconfont);
        setIcon.setTypeface(iconfont);
        kuiIcon.setTypeface(iconfont);

    }

    @Override
    protected void initData() {
        presenter = new MyFragmentPresenterImpl(new MyFragmentInteractorImpl(), this);
        dialog = new Dialog(mContext, R.style.Translucent_NoTitle);
    }

    @OnClick({R.id.set_layout, R.id.feedback_layout, R.id.login_layout, R.id.bank_layout,
            R.id.record_layout, R.id.message_layout, R.id.gold_tip, R.id.gold_container,
            R.id.invite, R.id.service, R.id.help, R.id.withdraw,R.id.custody,R.id.message_layout2,R.id.hintMsg
    })
    public void click(View v) {
        switch (v.getId()) {
            case R.id.set_layout:
                if (checkIsLogin()) {
                    bundle = new Bundle();
                    bundle.putString("idcard", idcard);
                    gotoActivity(mContext, SettingActivity.class, bundle);
                }
                break;
            case R.id.feedback_layout:
                if (checkIsLogin()) {
                    gotoActivity(mContext, FeedBackActivity.class, null);
                }

                break;
            case R.id.hintMsg:
                if (checkIsLogin()) {
                    gotoActivity(mContext, WithDrawRecordActivity.class, null);
                }

                break;
            case R.id.login_layout:
                //登录点击
                gotoActivity(mContext, LoginActivity.class, null);
                break;
            case R.id.custody:
                bundle=new Bundle();
                bundle.putString("type","8");
                bundle.putString("url",appJumpUrl);
                gotoActivity(mContext,WebViewActivity.class,bundle);
                break;
            case R.id.bank_layout:
                if (checkIsLogin()) {
                    gotoActivity(mContext, BankCardDisplayActivity.class, null);
                }
                break;
            case R.id.gold_container:
                if (checkIsLogin()) {
                    //奖励金
                    bundle = new Bundle();
                    bundle.putString("type", "4");
                    gotoActivity(mContext, WebViewActivity.class, bundle);
                }

                break;
            case R.id.gold_tip:
                showNoticeDialog();
                break;
            case R.id.record_layout:
                if (checkIsLogin()) {
                    gotoActivity(mContext, BorrowRecordActivity.class, null);
                }

                break;
            case R.id.message_layout:
                if (checkIsLogin()) {
                    gotoActivity(mContext, MessageActivity.class, null);
                }

                break;
            case R.id.message_layout2:
                if (checkIsLogin()) {
                    gotoActivity(mContext, MessageActivity.class, null);
                }

                break;
            case R.id.invite:
                if (checkIsLogin()) {
                    bundle = new Bundle();
                    bundle.putString("type", "5");
                    gotoActivity(mContext, WebViewActivity.class, bundle);
                }

                break;
            case R.id.service:
                if (checkIsLogin()) {
                    Information info = new Information();
                    info.setAppkey("fe1e7db48a5f47eaad3f947a21c61475");
                    info.setUid(loginPhone);
                    info.setInitModeType(2);
                    info.setSkillSetId("346fe9b1ab7f47c8ad3daddd66425152");
                    info.setTitleImgId(R.color.black);
                    info.setVisitTitle("蜡笔超卡");
                    SobotApi.startSobotChat(mContext, info);
                }
                break;
            case R.id.help:
                if (checkIsLogin()) {
                    bundle = new Bundle();
                    bundle.putString("type", "7");
                    gotoActivity(mContext, WebViewActivity.class, bundle);
                }
                break;
            case R.id.withdraw:
                if (TextUtils.isEmpty(isCanClick)){
                    ToastUtils.show("获取数据异常，请稍后重试！");
                    return;
                }
                if (isCanClick.equals("1")){
                    gotoActivity(mContext, WithdrawActivity.class, null);
                }else{
                    ToastUtils.show(clickMsg);
                }

                break;
        }
    }

    private void showNoticeDialog() {
        final LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.dialog_gold_tip, null);
        dialog.setContentView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);

        TextView ok = v.findViewById(R.id.ok_layout);

        ok.setOnClickListener((view) -> {
            dialog.dismiss();
        });

        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        dialog.onWindowAttributesChanged(wl);
        dialog.setCanceledOnTouchOutside(false);

        dialog.show();

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.init();
        setLoginMethod();
    }

    @Override
    public void showProgress() {
        showLoadingProgress();
    }

    @Override
    public void hideProgress() {
        hideLoadingPregress();
    }

    @Override
    public void showMessage(String str) {
        ToastUtils.show(str);
    }

    @Override
    public void setData(OverdueAndIdcardResponse response) {
        String overdue = response.getData().getOverdue();
        String messageNum = response.getData().getMessageNum();
        idcard = response.getData().getIdcard();
        String rewardGold = response.getData().getRewardGold();
        if (!TextUtils.isEmpty(overdue) && overdue.equals("1")) {
            overdueImg.setVisibility(View.VISIBLE);
        }

        if (!TextUtils.isEmpty(rewardGold)) {
            goldLayout.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(messageNum) && messageNum.equals("0")) {
            msgIcon.setImageResource(R.mipmap.msg_nomal);
            msgIcon2.setImageResource(R.mipmap.msg_nomal);
        } else {
            msgIcon.setImageResource(R.mipmap.msg_notice);
            msgIcon2.setImageResource(R.mipmap.msg_notice);
        }
    }

    @Override
    public void getDepositBalanceInfoCallback(GetDepositBalanceInfo response) {
        if (response == null) {
            ToastUtils.show("获取数据异常，请稍后重试！");
            return;
        }
        String accOpenStatus = response.getData().getAccOpenStatus();
        if (TextUtils.isEmpty(accOpenStatus)) {
            ToastUtils.show("获取数据异常，请稍后重试！");
            return;
        }
        if (accOpenStatus.equals("1")) {
            //已开户
            topLayout1.setVisibility(View.GONE);
            topLayout2.setVisibility(View.VISIBLE);
            view.setVisibility(View.VISIBLE);
            custody.setVisibility(View.VISIBLE);
            String amountStr=response.getData().getAmount();
            isCanClick=response.getData().getIsCanClick();
            clickMsg=response.getData().getClickMsg();
            appJumpUrl=response.getData().getAppJumpUrl();
            if (!TextUtils.isEmpty(amountStr)){
                amount.setText(DetectionUtil.addComma(amountStr));
            }
            String hintStr=response.getData().getHintMsg();
            if (!TextUtils.isEmpty(hintStr)){
                hintMsg.setText(hintStr);
                hintMsg.setVisibility(View.VISIBLE);
            }else{
                hintMsg.setVisibility(View.GONE);
            }

        } else {
            topLayout1.setVisibility(View.VISIBLE);
            topLayout2.setVisibility(View.GONE);
            view.setVisibility(View.GONE);
            custody.setVisibility(View.GONE);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void setLoginMethod() {
        if (MyApplication.getInstance().isLogin()) {
            loginPhone = SharedPreferencesManager.getInstance().getString("phone", "");
            if (!TextUtils.isEmpty(loginPhone)) {
                presenter.getDepositBalanceInfo();
                txt_user.setText(loginPhone.substring(0, 3) + "****" + loginPhone.substring(7, 11));
                txtUser2.setText(loginPhone.substring(0, 3) + "****" + loginPhone.substring(7, 11));
                loginLayout.setEnabled(false);
                loginLayout2.setEnabled(false);
            } else {
                txt_user.setText("未登录");
                txtUser2.setText("未登录");
                loginLayout.setEnabled(true);
                loginLayout2.setEnabled(true);
                topLayout1.setVisibility(View.VISIBLE);
                topLayout2.setVisibility(View.GONE);
            }


        } else {
            txt_user.setText("未登录");
            txtUser2.setText("未登录");
            loginLayout.setEnabled(true);
            loginLayout2.setEnabled(true);
            topLayout1.setVisibility(View.VISIBLE);
            topLayout2.setVisibility(View.GONE);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    @Subscribe
    public void getEventBus(Integer num){
        if (num==1){
            presenter.init();
            setLoginMethod();
        }
    }
}
